﻿//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 工具栏插件类；
*/



define(function (require, exports, module) {
    FXREADER.CToolBarPlugin = function (pReaderApp) {
        var _pReaderApp = pReaderApp,
            _bInited = false,
            _Language = _pReaderApp.GetLanguage(),
             _self = this;

        //获取Plugin名称;
        this.GetPluginName = function () {
            return "ToolBar Plugin";
        };

        //插件被注册的时候调用;
        this.PIOnRegister = function () {
            if (FXREADER.Config.ToolBar == null)
                return false;
            if (_bInited)
                return true;
            _self.CreateCmdAndCmdUI();
            var jqTopPanel = $("#" + _pReaderApp.GetTopPanelID());
            jqTopPanel.show();
            jqTopPanel.append(_self.CreateToolBarHtml());
        };

        //模块初始化;
        this.PIInit = function () {
            if (FXREADER.Config.ToolBar == null)
                return false;
            if (_bInited)
                return true;
     //       _self.CreateCmdAndCmdUI();
            var jqTopPanel = $("#" + _pReaderApp.GetTopPanelID());
            jqTopPanel.html("");
            jqTopPanel.append(_self.CreateToolBarHtml());
            _self.InitToolBarUI();

            $('.fwr_toolbar_btn').live('mousedown', function (event) {
                try {
                    if (event.button == 0 || event.button == 1) {
                        var cmdName = event.target.getAttribute("cmd");
                        _pReaderApp.ExecCommand(cmdName, null);
                    }
                }
                catch (ex) {
                }
            });
            _pReaderApp.OnResize();
            _pReaderApp.StepProgress();
            _bInited = true;
        };

        this.CreateToolBarHtml = function () {
            var toolbarHtml = "<div id='readertoolbar' class='fwr_toolbar' style='width:100%'>";
            var iToolsNum = FXREADER.Config.ToolBar.length;
            for (var i = 0; i < iToolsNum; i++) {
                var childBar = FXREADER.Config.ToolBar[i];
                var childbarHtml = "";
                if (childBar.FloatRight != null && childBar.FloatRight === true) {
                    childbarHtml = "<div id='tool_" + childBar.name + "' class='fwr_toolbar_block_right' style=''>";
                }
                else {
                    childbarHtml = "<div id='tool_" + childBar.name + "' class='fwr_toolbar_block_left' style=''>";
                }


                var btns = childBar.btns;
                var iChildBarLen = btns.length;
                var bHasChild = false;
                for (var j = 0; j < iChildBarLen; j++) {
                    var btnName = btns[j];
                    var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                    if (pCommandUI == null)
                        continue;
                    if (btnName != "ToolBarSeparator")
                        bHasChild = true;
                    childbarHtml += pCommandUI.CreateHtml();
                }
                if (bHasChild) {
                    childbarHtml += "</div>";
                    toolbarHtml += childbarHtml;
                }

            }
            toolbarHtml += "</div>";
            return toolbarHtml;
        };

        this.CreateCmdAndCmdUI = function () {
            //添加分割符;
            _pReaderApp.AddCommandUI({
                UIName: "ToolBarSeparator",
                CommandName: null,
                CreateHtmlContentFunc: function () {
                    return "<div class='fwr_toolbar_separator'></div>";
                }
            });
            //添加Log;
            _pReaderApp.AddCommandUI({
                UIName: "FoxitLog",
                CommandName: null,
                CreateHtmlContentFunc: function () {
                    return "<img  src='./images/foxitlogo.png' style='margin-left:5px;margin-top:8px; width:207px; height:16px;float:left'  />";
                }
            });

            //添加Log;
            _pReaderApp.AddCommandUI({
                UIName: "FoxitLog_Right",
                CommandName: null,
                CreateHtmlContentFunc: function () {
                    return "<a id='btnBackFoxit' href='" + FXREADER.Config.MainUrl + "' class='fwr_toolbar_goto_foxitcloud fwr_toolbar_btn'  style='margin-right:20px;height:16px;float:right' >" + _Language.ToolBar.MainUrlTipInfo + "</a>";
                }
            });

           
        };

        //模块卸载;
        this.PIUnload = function () {
        };

        this.GetReaderApp = function () {
            return _pReaderApp;
        };
    };

    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pToolBarPlugin = new FXREADER.CToolBarPlugin(pReaderApp);
        return pToolBarPlugin;
    };
});